package member

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/cars"
	"parking-lot-go-admin/model/member"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var memberStruct member.MemberStruct
	var memberData []member.MemberStruct
	model.GetModelData(
		model.Db,
		memberStruct.TableName(),
		&memberData,
		model.QueryOptions{
			Conditions: where,
			OrderBy:    "id desc",
			Page:       page,
			PageSize:   pageSize,
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range memberData {
		var carsStruct cars.CarsStruct
		var carsData []cars.CarsStruct

		model.GetModelData(
			model.Db,
			carsStruct.TableName(),
			&carsData,
			model.QueryOptions{
				Conditions: map[string]interface{}{
					"tripartite_type": 3,
					"member_id":       v.Id,
				},
				OrderBy:  "id desc",
				Page:     page,
				PageSize: pageSize,
				Debug:    true,
			},
		)

		var cars []string
		for _, v := range carsData {
			cars = append(cars, v.CarNumber)
		}

		returnData = append(returnData, map[string]interface{}{
			"mobile":    v.Mobile,
			"real_name": v.RealName,
			"remarks":   v.Remarks,
			"state":     v.State,
			"money":     v.Money,
			"cars":      cars,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		memberStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
